/*
   Copyright (c) 2023 李伟国
   learner-platform is licensed under Mulan PSL v2.
   You can use this software according to the terms and conditions of the Mulan PSL v2. 
   You may obtain a copy of Mulan PSL v2 at:
               http://license.coscl.org.cn/MulanPSL2 
   THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.  
   See the Mulan PSL v2 for more details. 
*/

package cn.platform.service.impl;

import cn.platform.constant.EssayConstant;
import cn.platform.entity.Essay;
import cn.platform.mapper.EssayMapper;
import cn.platform.page.PageFactory;
import cn.platform.service.IEssayService;
import cn.platform.utils.UserUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class EssayServiceImpl extends ServiceImpl<EssayMapper, Essay> implements IEssayService, EssayConstant {

    /**
     * 发布文章
     *
     * @param essay 文章
     * @return 是否保存成功
     */
    @Override
    public Boolean releaseArticle(Essay essay) {
        essay.setCategories(CATEGORIES_ARTICLE);
        essay.setStatus(STATUS_RELEASED);
        essay.setReleaseTime(new Date());
        essay.setAuthorId(UserUtil.getUserId());
        System.out.println(essay);
        return this.baseMapper.insert(essay) > 0;
    }

    /**
     * 保存文章为草稿
     *
     * @param essay 文章
     * @return 是否保存成功
     */
    @Override
    public Boolean draftArticle(Essay essay) {
        essay.setCategories(CATEGORIES_ARTICLE);
        essay.setStatus(STATUS_DRAFT);
        essay.setAuthorId(UserUtil.getUserId());
        return this.baseMapper.insert(essay) > 0;
    }

    /**
     * 保存题解
     *
     * @param essay 题解
     * @return 是否保存成功
     */
    @Override
    public Boolean saveNote(Essay essay) {
        essay.setCategories(CATEGORIES_NOTE);
        return this.baseMapper.insert(essay) > 0;
    }

    /**
     * 通过分类查找文章
     *
     * @param classifications 类别
     * @return 文章列表
     */
    @Override
    public Page<Essay> findArticleListByClassification(String... classifications) {
        return this.baseMapper.selectByCategoriesAndClassifications(PageFactory.defaultPage(), CATEGORIES_ARTICLE, classifications);
    }

    /**
     * 通过标签查找文章
     *
     * @param labels 标签
     * @return 文章列表
     */
    @Override
    public Page<Essay> findArticleListByLabel(String... labels) {
        return this.baseMapper.selectByCategoriesAndLabels(PageFactory.defaultPage(), CATEGORIES_ARTICLE, labels);
    }

    /**
     * 通过作者id查找文章
     *
     * @param authorId 作者id
     * @return 文章列表
     */
    @Override
    public Page<Essay> findArticleListByAuthorId(Long authorId) {
        return this.baseMapper.selectByCategoriesAndAuthorId(PageFactory.defaultPage(), CATEGORIES_ARTICLE, authorId);
    }

    /**
     * 作者查看自己的文章
     *
     * @param authorId 作者id
     * @return 文章列表
     */
    @Override
    public Page<Essay> findArticleListByAuthorIdPrinciple(Long authorId) {
        return this.baseMapper.selectByCategoriesAndAuthorIdPrinciple(PageFactory.defaultPage(),CATEGORIES_ARTICLE, authorId);
    }

    /**
     * 通过分类查找题解
     *
     * @param classification 分类
     * @return 题解列表
     */
    @Override
    public Page<Essay> findNoteListByClassification(String... classification) {
        return null;
    }

    /**
     * 通过标签查找题解
     *
     * @param label 标签
     * @return 题解列表
     */
    @Override
    public Page<Essay> findNoteListByLabel(String... label) {
        return null;
    }

    /**
     * 通过作者id查找题解
     *
     * @param authorId 作者id
     * @return 题解列表
     */
    @Override
    public Page<Essay> findNoteListByAuthorId(Long authorId) {
        return null;
    }

    /**
     * 删除文章
     *
     * @param essay 文章
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteEssay(Essay essay) {
        return this.baseMapper.updateStatusById(essay.getId(), STATUS_DELETED) > 0;
    }

    @Override
    public Page<Essay> findEssayList() {
        return null;
    }

    /**
     * 返回推荐文章
     *
     * @return 推荐文章
     */
    @Override
    public Page<Essay> findArticleRecommend() {
        return this.baseMapper.selectAll(PageFactory.defaultPage());
    }

    @Override
    public Page<Essay> findArticleByHeadline(String headline) {
        return this.baseMapper.selectByCategoriesAndHeadline(PageFactory.defaultPage(),CATEGORIES_ARTICLE,headline);
    }
}
